//
//  FirstSectionHeaderView.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/8.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "FirstSectionHeaderView.h"

@interface FirstSectionHeaderView ()

@property (nonatomic, retain) BaseImageView *imgBack;
@property (nonatomic, retain) BaseImageView *imgTitle;
@property (nonatomic, retain) BaseLabel *labelTitle;
@property (nonatomic, retain) BaseLabel *labelContent;

@end

@implementation FirstSectionHeaderView

- (void)dealloc
{
    [_imgBack release];
    [_imgTitle release];
    [_labelTitle release];
    [_labelContent release];
    [_dicTop release];
    [super dealloc];
}
#pragma mark - ------------ 初始化 ---------------
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
     
        [self creatSub];
    }
    return self;
}

- (void)creatSub
{

    self.imgBack = [[BaseImageView alloc]init];
    [self addSubview:_imgBack];
   //毛玻璃
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    //创建个视图把效果加上.
    UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:effect];
    //设置尺寸.
    view.frame = self.bounds;
    [self.imgBack addSubview:view];
    [view release];
    
    self.imgTitle = [[BaseImageView alloc]init];
    _imgTitle.layer.borderWidth = 2;
    _imgTitle.layer.borderColor = [UIColor whiteColor].CGColor;
    [self addSubview:_imgTitle];
    
    self.labelTitle = [[BaseLabel alloc]init];
    [self addSubview:_labelTitle];
    _labelTitle.textAlignment = NSTextAlignmentCenter;
    _labelTitle.font = [UIFont systemFontOfSize:20];
    _labelTitle.font = [UIFont boldSystemFontOfSize:20];
    _labelTitle.textColor = [UIColor whiteColor];
    
    self.labelContent = [[BaseLabel alloc]init];
    [self addSubview:_labelContent];
    _labelContent.numberOfLines = 0;
    _labelContent.font = [UIFont systemFontOfSize:17];
    _labelContent.font = [UIFont boldSystemFontOfSize:15];
    _labelContent.textAlignment = NSTextAlignmentCenter;
    _labelContent.textColor = [UIColor whiteColor];
    
    
    //_imgBack.backgroundColor = [UIColor redColor];
   // _imgTitle.backgroundColor = [UIColor yellowColor];
   // _labelContent.backgroundColor = [UIColor grayColor];
   // _labelTitle.backgroundColor = [UIColor yellowColor];
    [_imgBack release];
    [_imgTitle release];
    [_labelTitle release];
    [_labelContent release];

    
}

#pragma mark - ------------ 赋值 ---------------
- (void)setDicTop:(NSDictionary *)dicTop
{
    if (_dicTop != dicTop) {
        
        [_dicTop release];
        _dicTop = [dicTop retain];

        [_imgBack sd_setImageWithURL:[NSURL URLWithString:_dicTop[@"image"]] placeholderImage:PLACEHOLDER];
        [_imgTitle sd_setImageWithURL:[NSURL URLWithString:_dicTop[@"image"]] placeholderImage:PLACEHOLDER];
        _labelTitle.text = _dicTop[@"name"];
        _labelContent.text = _dicTop[@"desc"];
    }
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _imgBack.translatesAutoresizingMaskIntoConstraints = NO;
    _imgTitle.translatesAutoresizingMaskIntoConstraints = NO;
    _labelTitle.translatesAutoresizingMaskIntoConstraints = NO;
    _labelContent.translatesAutoresizingMaskIntoConstraints = NO;
    

    
    [_imgBack mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.bottom.right.mas_equalTo(self).offset(0);
    }];
    
    
    [_imgTitle mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self).offset(40);
        make.centerX.mas_equalTo(self);
        make.width.height.mas_equalTo(100);
    }];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgTitle.mas_bottom).offset(10);
        make.centerX.mas_equalTo(_imgTitle);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
    }];

    CGFloat h = [[self class]heightWithText:_labelContent.text];
    
    [_labelContent mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_labelTitle.mas_bottom).offset(5);
        make.centerX.mas_equalTo(_imgTitle);
        make.width.mas_equalTo(SCREEN_WIDTH - 40);
        make.height.mas_equalTo(h);
    }];
    
}


#pragma mark - ------------ 文本高度自适应 ---------------
+ (CGFloat)heightWithText:(NSString *)text
{
    
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:18]} context:nil];
    
    return rect.size.height;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
